/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.controller;

import cn.webrx.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: smvc2022 - HomeController
 * <p>Powered by webrx On 2022-04-07 17:46:46
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@RestController
public class HomeController {
    @Autowired
    private JwtUtils ju;

    @Autowired
    private HttpServletRequest req;

    @RequestMapping
    public String index() {
        return "index 项目首页";
    }

    @RequestMapping("/ass")
    public Map<String, Object> saves() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "保存成功");
        return map;
    }


    @RequestMapping("/asave")
    public Map<String, Object> save(@RequestHeader(name = "token", required = false, defaultValue = "") String token) {
        Map<String, Object> map = new HashMap<String, Object>();
        if ("".equalsIgnoreCase(token)) {
            map.put("code", 405);
            map.put("msg", "无操作，请登录");
        } else if (ju.verifyToken(token)) {
            map.put("code", 200);
            map.put("msg", "保存成功");
        } else {
            map.put("code", 405);
            map.put("msg", "无法操作，请重新登录");
        }
        return map;
    }

    @RequestMapping("/aedit")
    public Map<String, Object> edit() {
        Map<String, Object> map = new HashMap<>();
        if ("".equalsIgnoreCase(req.getHeader("token")) || req.getHeader("token") == null) {
            map.put("code", 405);
            map.put("msg", "无操作，请登录" + req.getMethod());
        } else if (ju.verifyToken(req.getHeader("token"))) {
            map.put("code", 200);
            map.put("msg", "删除数据成功" + req.getMethod());
        } else {
            map.put("code", 405);
            map.put("msg", "无法操作，请重新登录" + req.getMethod());
        }
        return map;
    }

    @RequestMapping("/adel")
    public Map<String, Object> del(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        if ("".equalsIgnoreCase(req.getHeader("token")) || req.getHeader("token") == null) {
            map.put("code", 405);
            map.put("msg", "无操作，请登录");
        } else if (ju.verifyToken(req.getHeader("token"))) {
            map.put("code", 200);
            map.put("msg", "删除数据成功");
        } else {
            map.put("code", 405);
            map.put("msg", "无法操作，请重新登录");
        }
        return map;
    }
}
